﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_QuanLyTinTuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataToTreeView();
            BindGrid();
        }
    }
    private void BindGrid()
    {
        string dv = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        _dbDataContext _db = new _dbDataContext();
        var list = from p in _db.tblNews
                   where p.tblAcount.DonVi.Trim() == dv orderby p.ID descending
                   select new MemberShip_ASP.ItemNews
                   {
                       ID = p.ID,
                       Title = p.TieuDe.Trim(),
                       NguoiDang = p.tblAcount.TenHienThi.Trim(),
                       NgayDang = string.Format("{0:dd/MM/yyyy}", p.NgayDang),
                       Loai = p.Loai
                   };
        grid_news.DataSource = list.ToList();
        grid_news.DataBind();
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in grid_news.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    protected void grid_news_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i <= grid_news.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)grid_news.Rows[i].FindControl("lblloai");

            if (lblparent.Text == "1")
            {
                grid_news.Rows[i].Cells[6].Text = "<button class='btn btn-circle btn-success show-tooltip' data-original-title='Tin tức'><i class='icon-file-text-alt'></i></button>";
            }
            else if (lblparent.Text == "2")
            {
                grid_news.Rows[i].Cells[6].Text = "<button class='btn btn-circle  btn-primary show-tooltip' data-original-title='Thông báo'><i class= 'icon-envelope-alt'></i></button>";
            }
        }
    }
    protected void grid_news_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = grid_news.DataKeys[e.RowIndex].Value.ToString();
        bool success = CatalogAccess.DeleteNews(int.Parse(id)); //xoa tin duoc chon trong database
        BindGrid();
    }
    protected void btn_addnews_Click(object sender, EventArgs e)
    {
        pneditnews.Visible = true;
        pnmanager.Visible = false;
        lbltitle.Text = "Đăng tin mới";
        txttitle.Text = "";
        editor.Content = "";
        lblstatus.Text = "";
        lblmessage.Text = "";
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
        //lay du lieu moi dua vao listnews
        if (lblstatus.Text == "co")
        {
            BindGrid();
        }
        pneditnews.Visible = false;
        pnmanager.Visible = true;
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (txttitle.Text.Length == 0)
        {
            lblmessage.Visible = true;
            lblmessage.Text = "Nhập tiêu đề.";
            return;
        }
        if (txtid.Value == "") //trung hop them moi
        {
            List<string> listmadonvi = new List<string>();
            if (rdball.Checked == false) //trung hop chon don vi de goi
            {
                TreeNode tNode = TreeView1.Nodes[0];
                if (tNode.Checked == true)
                {
                    listmadonvi.Add(tNode.Value.ToString());
                }
                foreach (TreeNode item in tNode.ChildNodes)
                {
                    if (item.Checked == true)
                    {
                        listmadonvi.Add(item.Value.ToString());
                    }
                    foreach (TreeNode items in item.ChildNodes)
                    {
                        if (items.Checked == true)
                        {
                            listmadonvi.Add(items.Value.ToString());
                        }
                    }
                }
            }
            else //truong hop goi cho tat ca don vi
            {
                TreeNode tNode = TreeView1.Nodes[0];
                listmadonvi.Add(tNode.Value.ToString());
                foreach (TreeNode item in tNode.ChildNodes)
                {
                    listmadonvi.Add(item.Value.ToString());
                    foreach (TreeNode items in item.ChildNodes)
                    {
                        listmadonvi.Add(items.Value.ToString());
                    }
                }
            }
            string st = "";
            foreach (string item in listmadonvi)
            {
                st += item + ";";
            }
            int nguoigui = CatalogAccess.GetIDAcountWithUserName(this.Page.User.Identity.Name);
            bool success = CatalogAccess.InsertNews((tintuc.Checked == true ? 1 : 2), txttitle.Text, editor.Content, nguoigui, st);
            if (success)
            {
                lblmessage.Visible = true;
                lblmessage.Text = "Đã thêm thành công.";
                txttitle.Text = "";
                editor.Content = "";
                txttitle.Focus();
                lblstatus.Text = "co";
            }
        }
        else//truong hop update
        {
            _dbDataContext _db = new _dbDataContext();
            bool success = CatalogAccess.UpdateNews(int.Parse(txtid.Value), (tintuc.Checked == true ? 1 : 2), txttitle.Text, editor.Content);
            if (success)
            {
                lblmessage.Visible = true;
                lblmessage.Text = "Cập nhật thành công.";
                lblstatus.Text = "co";
            }
        }
    }
    protected void grid_news_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditNews")
        {
            pneditnews.Visible = true;
            pnmanager.Visible = false;
            lbltitle.Text = "Sửa tin";
            _dbDataContext _db = new _dbDataContext();
            var val = _db.tblNews.Single(r => r.ID == int.Parse(e.CommandArgument.ToString()));
            txtid.Value = val.ID.ToString();
            txttitle.Text = val.TieuDe;
            if (val.Loai == 1)
                tintuc.Checked = true;
            else
                thongbao.Checked = true;
            editor.Content = val.NoiDung;
            lblstatus.Text = "";
            lblmessage.Text = "";
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in grid_news.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = grid_news.DataKeys[dr.RowIndex].Value.ToString().Trim();
                bool success = CatalogAccess.DeleteNews(int.Parse(id)); //xoa tin duoc chon trong database
            }
        }
        BindGrid();
    }
    protected void linkDoanVien_Click(object sender, EventArgs e)
    {
        string dv = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        _dbDataContext _db = new _dbDataContext();
        var list = from p in _db.tblNews
                   where p.tblAcount.DonVi.Trim() == dv && p.Loai==1
                   select new MemberShip_ASP.ItemNews
                   {
                       ID = p.ID,
                       Title = p.TieuDe.Trim(),
                       NguoiDang = p.tblAcount.TenHienThi.Trim(),
                       NgayDang = string.Format("{0:dd/MM/yyyy}", p.NgayDang),
                       Loai = p.Loai
                   };
        grid_news.DataSource = list.ToList().OrderByDescending(r => r.ID);
        grid_news.DataBind();
    }
    protected void linkDoanVienTruongThanh_Click(object sender, EventArgs e)
    {
        string dv = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        _dbDataContext _db = new _dbDataContext();
        var list = from p in _db.tblNews
                   where p.tblAcount.DonVi.Trim() == dv && p.Loai == 2
                   select new MemberShip_ASP.ItemNews
                   {
                       ID = p.ID,
                       Title = p.TieuDe.Trim(),
                       NguoiDang = p.tblAcount.TenHienThi.Trim(),
                       NgayDang = string.Format("{0:dd/MM/yyyy}", p.NgayDang),
                       Loai = p.Loai
                   };
        grid_news.DataSource = list.ToList().OrderByDescending(r => r.ID);
        grid_news.DataBind();
    }
    protected void btn_LocXemTatCa_Click(object sender, EventArgs e)
    {
        string dv = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        _dbDataContext _db = new _dbDataContext();
        var list = from p in _db.tblNews
                   where p.tblAcount.DonVi.Trim() == dv
                   select new MemberShip_ASP.ItemNews
                   {
                       ID = p.ID,
                       Title = p.TieuDe.Trim(),
                       NguoiDang = p.tblAcount.TenHienThi.Trim(),
                       NgayDang = string.Format("{0:dd/MM/yyyy}", p.NgayDang),
                       Loai = p.Loai
                   };
        grid_news.DataSource = list.ToList().OrderByDescending(r => r.ID);
        grid_news.DataBind();
    }
    private void BindDataToTreeView()
    {
        _dbDataContext _db = new _dbDataContext();
        string tendonvi = null;
        string madonvi = MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        var listcosodoan = _db.tblCoSoDoans.Where(r => r.MaDonVi.Trim() != "TDB");
        string daidien = madonvi.Substring(0, 3);
        //huyện đoàn,đoàn ngang cấp:cơ quan đoàn khối
        if (daidien == "CSD")
        {
            TreeView1.Nodes.Clear();
            tendonvi = listcosodoan.Single(r => r.MaDonVi.Trim() == madonvi.Trim()).TenDonVi;
            List<tblCoSoDoan> list = listcosodoan.Where(r => r.TrucThuoc.Trim() == madonvi).ToList();
            TreeNode root = new TreeNode(" <i class='icon-circle-arrow-right' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + tendonvi + "</span>", madonvi.Trim());
            foreach (tblCoSoDoan item in list)
            {
                TreeNode child = new TreeNode(" <i class=' icon-leaf' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item.TenDonVi.Trim() + "</span>", item.MaDonVi.Trim());
                List<tblCoSoDoan> list1 = listcosodoan.Where(r => r.TrucThuoc.Trim() == item.MaDonVi.Trim()).ToList();
                root.ChildNodes.Add(child);
                foreach (tblCoSoDoan item1 in list1)
                {
                    TreeNode child1 = new TreeNode(" <i class='icon-circle-arrow-right' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item1.TenDonVi.Trim() + "</span>", item1.MaDonVi.Trim());
                    child.ChildNodes.Add(child1);
                }


            }
            TreeView1.Nodes.Add(root);
        }
        else
        {
            if (daidien == "DCS" || daidien == "CDC")
            {
                //doan quan huyen
                TreeView1.Nodes.Clear();
                tendonvi = listcosodoan.Single(r => r.MaDonVi.Trim() == madonvi.Trim()).MaDonVi;
                List<tblCoSoDoan> list = listcosodoan.Where(r => r.TrucThuoc.Trim() == madonvi).ToList();
                TreeNode root = new TreeNode(" <i class='icon-circle-arrow-right' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + tendonvi + "</span>", madonvi.Trim());
                foreach (tblCoSoDoan item in list)
                {
                    TreeNode child = new TreeNode(" <i class='icon-circle-arrow-right' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item.TenDonVi.Trim() + "</span>", item.MaDonVi.Trim());
                    List<tblCoSoDoan> list1 = listcosodoan.Where(r => r.TrucThuoc.Trim() == item.MaDonVi.Trim()).ToList();
                    root.ChildNodes.Add(child);
                }
                TreeView1.Nodes.Add(root);
            }
            else
            {
                if (daidien == "CDD")
                {
                    //chi đoàn
                    TreeView1.Nodes.Clear();
                    tendonvi = listcosodoan.Single(r => r.MaDonVi.Trim() == madonvi.Trim()).TenDonVi.Trim().ToString();
                    List<tblCoSoDoan> list = listcosodoan.Where(r => r.TrucThuoc.Trim() == madonvi).ToList();
                    foreach (tblCoSoDoan item in list)
                    {
                        TreeNode root = new TreeNode(" <i class='icon-circle-arrow-right' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + tendonvi + "</span>", madonvi.Trim());
                        TreeView1.Nodes.Add(root);
                    }

                }
                else if (daidien == "TDB")
                {
                    TreeView1.Nodes.Clear();
                    tendonvi = "Tỉnh Đoàn Bình Dương";
                    List<tblCoSoDoan> list = listcosodoan.Where(r => r.TrucThuoc.Trim() == madonvi).ToList();
                    TreeNode root = new TreeNode(" <i class='icon-home' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + tendonvi + "</span>", madonvi.Trim());
                    foreach (tblCoSoDoan item in list)
                    {
                        TreeNode child = new TreeNode(" <i class=' icon-leaf' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item.TenDonVi.Trim() + "</span>", item.MaDonVi.Trim());
                        List<tblCoSoDoan> list1 = listcosodoan.Where(r => r.TrucThuoc.Trim() == item.MaDonVi.Trim()).ToList();
                        root.ChildNodes.Add(child);
                        foreach (tblCoSoDoan item1 in list1)
                        {
                            TreeNode child1 = new TreeNode(" <i class='icon-pencil' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item1.TenDonVi.Trim() + "</span>", item1.MaDonVi.Trim());
                            child.ChildNodes.Add(child1);
                            List<tblCoSoDoan> list2 = listcosodoan.Where(r => r.TrucThuoc.Trim() == item1.MaDonVi.Trim()).ToList();
                            foreach (tblCoSoDoan item2 in list2)
                            {
                                TreeNode child2 = new TreeNode(" <i class='icon-pencil' style=color:Green;font-size:14px;'></i><span style=';font-size:14px;'> " + item2.TenDonVi.Trim() + "</span>", item2.MaDonVi.Trim());
                                child1.ChildNodes.Add(child2);
                            }
                        }
                    }
                    TreeView1.Nodes.Add(root);
                }
            }
        }
        TreeView1.ExpandAll();
    }
    protected void rdball_CheckedChanged(object sender, EventArgs e)
    {
        pndonvi.Visible = !rdball.Checked;
    }
}